<template>
  <div class="content">
    <el-breadcrumb separator-class="el-icon-arrow-right" class="mbx">
      <el-breadcrumb-item :to="'/components/main'">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户搜索</el-breadcrumb-item>
    </el-breadcrumb>

    <div v-for="(item, index) in list" :key="index" class="Datalist">
      <div class="filename">
        <span>文件名称:</span>
        <span v-html="item.filename"></span>
      </div>
      <el-row type="flex" class="row-bg">
        <div class="uploadInstitution jigou">
          <span>上传机构:</span>{{ item.uploadInstitution }}
        </div>
        <div class="uploadInstitution jigou">
          <span>上传时间:</span>{{ item.uploadTime }}
        </div>
      </el-row>

      <el-row type="flex" class="row-bg">
        <div class="uploadInstitution jigou">
          <span class="bold">所属地区:</span>
          <span v-html="item.district"></span>
        </div>
        <div class="uploadInstitution jigou">
          <span class="bold">所属专题:</span>
          <!--<span>所属专题:</span>{{ item.topic | topicFormat }}-->
          <span v-html="topics[item.topic]"></span>
        </div>
      </el-row>
      <div class="uploadInstitution">
        <span class="bold">文件摘要:</span>
        <span v-html="item.summary"></span>
      </div>

      <el-row type="flex" class="row-btn">
        <el-button
          type="primary"
          icon="el-icon-document"
          round
          size="small"
          @click="opendialog(item.id)"
          >详细信息</el-button
        >

        <el-button
          type="warning"
          icon="el-icon-download"
          round
          size="small"
          @click="fileDownloadById(item.id)"
          >下载</el-button
        >
      </el-row>

      <!-- 点击弹框查看详情 -->
      <el-dialog
        title="查看详情"
        :visible.sync="dialogVisible"
        width="40%"
        class="dialog"
      >
        <el-table :data="gridData">
          <el-table-column property="date" width="140"></el-table-column>
          <el-table-column property="name" width=""></el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <!-- <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button> -->
        </span>
      </el-dialog>
    </div>

    <el-pagination
      class="paging"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage1"
      :page-size="pagesize"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { search } from "../../Api/api"; //搜索
import { gettopicdetailData } from "../../Api/api"; //查看详情
import { gettopicuploadData } from "../../Api/api"; //下载
import bus from "../../Api/bus.js";

import axios from "axios";

export default {
  filters: {
    topicFormat(value) {
      let text =
        value === "0"
          ? "防洪抗旱"
          : value === "1"
          ? "河流水系"
          : value === "2"
          ? "水环境"
          : value === "3"
          ? "水利工程"
          : value === "4"
          ? "水文"
          : value === "5"
          ? "水资源"
          : value === "6"
          ? "统计年鉴"
          : value === "7"
          ? "政策规划"
          : value === "8"
          ? "地理空间"
          : value === "9"
          ? "其他"
          : "";
      // let text = value === '0' ? '防洪抗旱' : (value === '1' ? '河流水系':(value === '2' ? '水环境' :'')),
      return text;
    },
  },
  data() {
    return {
      currentPage1: 1, //当前页数
      pagesize: 10, //    每页的数据
      total: 0, //默认数据总数
      gridData: [],
      dialogVisible: false,
      list: [],

      params: {
        startNum: 0,
        pagesize: 10,
        sortOrder: "",
        sortBy: "",
        //  query: "流量",
        query: "",
        topic: "",
      },
      topics: [
        "防洪抗旱",
        "河流水系",
        "水环境",
        "水利工程",
        "水文",
        "水资源",
        "统计年鉴",
        "政策规划",
        "地理空间",
        "其他",
      ],
    };
  },

  // mounted() {
  //   bus.$on("query", (msg) => {
  //     this.params.query = msg;
  //      this.init();
  //   });
  // },

  mounted() {
    let that = this;
    let routerParams = that.$route.query.query;
    that.params.query = routerParams;
    that.init();
    bus.$on("search", query => {
      that.params.query = query
      that.init();
    })
  },

  methods: {
    //显示列表信息
    init() {
      let that = this;
      // debugger
      search(that.params).then(function (res) {
        if (res.status === 200 && res.data.code === 0) {
          that.list = res.data.data;
          //分页总数
          that.total = res.data.num;
          if (res.data.num === 0) {
            that.$message("查询无结果");
          }
        }
      })
    },
    opendialog(id) {
      console.log(id);
      this.dialogVisible = true;
      let that = this;
      // 在這調用詳情數據
      gettopicdetailData({ id: id }).then(function (res) {
        if (res.status === 200 && res.data.code === 0) {
          that.gridData = [
            {
              date: "文件名称:",
              name: res.data.data.filename,
            },
            {
              date: "上传机构:",
              name: res.data.data.uploadInstitution,
            },
            {
              date: "上传时间",
              name: res.data.data.uploadTime,
            },
            {
              date: "文件摘要",
              name: res.data.data.summary,
            },
            {
              date: "所属地区",
              name: res.data.data.district,
            },
            {
              date: "所属专题",
              // name: res.data.data.topic === "0" ? "防洪抗旱" : "",
              name: that.topics[res.data.data.topic],
            },
            {
              date: "上传者",
              name: res.data.data.uploader,
            },
            {
              date: "文件统计起始时间",
              name: res.data.data.startTime,
            },
            {
              date: "文件统计终止时间",
              name: res.data.data.endTime,
            },
            {
              date: "文件大小",
              name: res.data.data.fileSize,
            },
            {
              date: "文件类型",
              name: res.data.data.fileType,
            },
          ];
        }
      });
    },
    //下载
    fileDownloadById(id) {
      let that = this;
      let url = "http://192.168.1.7:9000/download/ById?id=" + id;
      const a = document.createElement("a");
      a.style.display = "none";
      // a.download = filename;
      // console.log(a.download)
      a.href = url;
      a.click();
      // gettopicuploadData({id:id}).then(function (res) {
      //   console.log(res.data);
      // })
    },
    //分页
    handleSizeChange: function (currentPage) {
      currentPage = this.pagesize;
      console.log(this.pagesize);
    },
    handleCurrentChange: function (currentPage) {
      //点击第几页
      this.currentPage = currentPage;
      this.params.startNum = (this.currentPage - 1) * this.pagesize;
      //  this.params.pagesize=this.currentPage;
      this.init();
      console.log(this.currentPage); //点击第几页
    },
  },
};
</script>

<style scoped>
.content {
  width: 90%;
  margin: 0 auto;
}
.mbx {
  padding-top: 20px;
}
.filename {
  font-size: 16px;
  color: #0066cc;
  font-weight: bold;
}
.jigou {
  width: 260px;
}
.Datalist {
  border-bottom: 1px solid #e5e5e5;
  padding-bottom: 10px;
  padding-top: 30px;
}
.Datalist span {
  padding-right: 10px;
}
.bold {
  font-weight: bold;
}

.uploadInstitution {
  margin-top: 14px;
  margin-bottom: 10px;
  font-size: 14px;
  color: #666;
}
.row-btn {
  margin-top: 14px;
  margin-bottom: 10px;
}
</style>
<style>
.dialog .el-table thead {
  display: none;
}
.dialog .el-dialog__body {
  padding: 0px 20px;
}
.paging {
  text-align: center;
  margin-top: 30px;
}
</style>
